package com.chanyachting.emall.action.company;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.form.CompanyForm;
import com.chanyachting.emall.service.CompanyService;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.serviceImpl.CompanyServiceImpl;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.MyFile;
import com.chanyachting.emall.util.RequestUtil;


/**
 * 更新公司信息
 * @author robert
 *
 */

public class UpdateCompanyInfoAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		
		try{
			
			CompanyForm cform=(CompanyForm)form;
			
			Company company=new Company();
			
			BeanUtils.copyProperties(company, cform);
			
			String logoUrl=MyFile.saveFileForm(cform.getLogoFile(), request);  //上传公司logo
			
			String logoUrl2=MyFile.saveFileForm(cform.getLogoFile2(), request);  //上传营业执照图片
			
			String yellowPageUrl=MyFile.saveFileForm(cform.getYellowPageFile(), request);  //上传黄页图片 
			
			if(logoUrl!=null && !logoUrl.equals("")) company.setLogoUrl(logoUrl);
			
			if(logoUrl2!=null && !logoUrl2.equals("")) company.setLogoUrl2(logoUrl2);
			
			if(yellowPageUrl!=null && !yellowPageUrl.equals("")) company.setYellowPageImgUrl(yellowPageUrl);
			
			//测试代码
			company.setBusinessTypeId("0");
			company.setBusinessCategoryId("0");
			
			
			CompanyService cs=new CompanyServiceImpl();
			
			if(company.getId()!=0){
				
				cs.updateCompany(company);   //更新公司信息
				
			}else{
				
				company.setCreateDate(DateUtil.getDate());
			
				Member member=(Member)ru.getSessionObject(MemberService.MEMBER_IN_SESSION,new Member());
				
				cs.createCompany(company, member);  //创建公司信息
			}
			
			//重新从数据库中加载公司信息
			company=cs.getOneCompany(company);
			
			//保存公司对象到session
			ru.setSessionObject(CompanyService.COMPANY_IN_SESSION, company);
			
			ru.setSuccessMsg();
			
		}catch(Exception e){
			
			e.printStackTrace();
			
			ru.setFailMsg();
		}
		
		return mapping.findForward("companyUpdate");
	}
	
	
	

}
